﻿#ifndef CAMERATUCAM_PRIVATE_H
#define CAMERATUCAM_PRIVATE_H
#include "./sdk/inc/TUDefine.h"
#include <QThread>
#include "../CameraCommon/IndustryCamera.h"
#include "../CameraCommon/DialogCameraSetup.h"

namespace Qly
{
class AutoInit
{
public:
    AutoInit();
    ~AutoInit();
    int cameraCount() {return itApi.uiCamCount;}

    void cameraCreated() {cameraClassCount ++ ;}
    void cameraDeleted();
    void factoryCreated(){factoryClassCount ++;}
    void factoryDeleted();
private:
    int factoryClassCount = 0;
    int cameraClassCount = 0;
    TUCAM_INIT itApi;
};

AutoInit * getAutoInitInstance();



class ImageThread;
class CAMERA_EXPORT CameraTUCAM_p : public IndustryCamera
{
    friend ImageThread;
public:
    explicit CameraTUCAM_p(QObject *parent = nullptr);
    ~CameraTUCAM_p() Q_DECL_OVERRIDE;
    QString cameraName();
    QStringList resolution();
    bool setResolutionByIdx(int N);
    bool isLive() Q_DECL_OVERRIDE;
    bool imageSize(QSize &size, QSize &max, QSize &min);
    AVPixelFormat getBayer8Patern();

public slots:
    bool openByIdx(unsigned int index);
    bool openDefaultCamera() Q_DECL_OVERRIDE;
    bool openCameraByID(QString id);
    bool startGrabbing() Q_DECL_OVERRIDE;
    bool stopGrabbing() Q_DECL_OVERRIDE;
    bool setPixelType(AVPixelFormat type) Q_DECL_OVERRIDE;
    bool saveSettingsToFile(QString fileName) Q_DECL_OVERRIDE;
    bool loadSettingsFromFile(QString fileName) Q_DECL_OVERRIDE;
    void close() Q_DECL_OVERRIDE;

    bool setExposureTimeMode(AutoExposureMode mode) Q_DECL_OVERRIDE;
    bool exposureTimeMode(AutoExposureMode &mode) Q_DECL_OVERRIDE;

    bool setExposureTime(double us) Q_DECL_OVERRIDE;
    bool exposureTime(double &us) Q_DECL_OVERRIDE;
    bool exposureTimeRange(double &fMin, double &fMax, double &fValue) Q_DECL_OVERRIDE;
    bool showVCDPropertyPage() Q_DECL_OVERRIDE;

    bool frameRate(double &fps) Q_DECL_OVERRIDE;
    bool setFrameRate(double fps) Q_DECL_OVERRIDE;

    bool setAOIRegion(QRect rect);
    bool setAOIRegion(unsigned  int xOffset, unsigned  int yOffset, unsigned  int width, unsigned  int yHeight);
    QRect AOIRegion();
    bool setHeartBeatTimeout(int ms);

    bool getFlip(bool &fon, bool &hon);
    bool setVFlip(bool on);
    bool setHFlip(bool on);

private slots:
    void on_imageThreadFinished();
private:
    void fetchImageData();

    // CameraToupCamSettingDialog * m_dialog;

    DialogCameraSetup * m_dialog;
    TUCAM_OPEN m_opCam;
    TUCAM_FRAME m_frame;      // The frame object

    bool m_isLive;
    /*相机抓图回调*/
    static void OnCallbackBuffer(void *userContext);
    uint8_t * pImageBuffer;

    ImageThread * m_pThread;
};


}
#endif // CAMERATUCAM_PRIVATE_H
